package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.Utils.IDUtils;
import cn.com.yusys.yusp.admin.repository.mapper.CustCenterMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CustCenterService {

    @Resource
    CustCenterMapper centerMapper;

    @Transactional
    public Integer addNewCustomer(QueryModel model) {
        if (centerMapper.getCustomerCtfNo(model) >0){
            return null;
        }
        return centerMapper.addNewCustomer(model);
    }

    @Transactional
    public int excelAddNewCustomer(List<QueryModel> list) {
        return centerMapper.excelAddNewCustomer(list);
    }

    @Transactional
    public Integer updateCustomerImport(QueryModel model) {
        return centerMapper.updateCustomerImport(model);
    }

    @Transactional
    public Integer updateCustomerInformation(QueryModel model) {
        return centerMapper.updateCustomerInformation(model);
    }

    @Transactional
    public int deleteCustomer(List<Integer> list) {
        return centerMapper.deleteCustomer(list);
    }


    //根据用户的归属进行显示：查询条件为用户的id，根据id去统计出显示的内容
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getAllCustomerInformation(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, Object>> list = centerMapper.getAllCustomerInformation(model);
        PageHelper.clearPage();
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGridName(QueryModel model) {
        return centerMapper.getGridName(model);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getOrgName(QueryModel model) {
        return centerMapper.getOrgName(model);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getSecondGridName(QueryModel model) {
        return centerMapper.getSecondGridName(model);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCustomerManagerInformation(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, Object>> list = centerMapper.getCustomerManagerInformation(model);
        PageHelper.clearPage();
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getSubBranchManagerInformation(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, Object>> list = centerMapper.getSubBranchManagerInformation(model);
        PageHelper.clearPage();
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getBranchManagerInformation(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, Object>> list = centerMapper.getBranchManagerInformation(model);
        PageHelper.clearPage();
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCustomerInformation(QueryModel model) {
        return centerMapper.getCustomerInformation(model);
    }

    /**
     * 通过身份证去查询客户表中是否存在
     * 通过身份证信息去查询审批信息表中的审批状态为0或1的也不能通过
     * 都验证通过就插入进表中
     */
    @Transactional
    public ResultDto<Object> transferCustomer(QueryModel model) {
        ResultDto<Object> resultDto = new ResultDto<>();
        Map<String,Object> isExistCustomer = centerMapper.isExistCustomer(model);
        if (isExistCustomer ==null){
            resultDto.setCode(100);
            resultDto.setMessage("客户不存在，请先创建客户");
            return resultDto;
        }
        String appStatus = centerMapper.selectAppStatus(model);
        if (appStatus != null){
            if (appStatus.equals("0")){
                resultDto.setCode(200);
                resultDto.setMessage("该客户已在审批中");
                return resultDto;
            }else if (appStatus.equals("1")){
                resultDto.setCode(250);
                resultDto.setMessage("该客户已经审批过了");
                return resultDto;
            }
        }
        model.getCondition().put("id", IDUtils.getGuid());
        model.getCondition().put("custNo",isExistCustomer.get("custNo"));
        model.getCondition().put("custName",isExistCustomer.get("custName"));
        int insertUserTransfer = centerMapper.insertUserTransfer(model);
        model.getCondition().put("approverNo","1368");
        model.getCondition().put("approverName","庄文达");
        model.getCondition().put("approvalType","2");
        int insertProcessNode = centerMapper.insertProcessNode(model);
        if (insertUserTransfer == 1 && insertProcessNode == 1){
            resultDto.setCode(300);
            resultDto.setMessage("用户移交已发送");
        }
        return resultDto;
    }

    public List<String> selectExistCustInfo(List<String> list){
        return centerMapper.selectExistCustInfo(list);
    }
}
